import Toast from 'react-simple-toast';
import { text } from './data.js'
import createBrowserHistory from 'history/createBrowserHistory';
import axios from 'axios'
const history = createBrowserHistory();

// axios.interceptors.request.use(function(config) {
// 	toast.loading('正在加载')
// 	return config
// })

axios.interceptors.response.use(function(response) {
	if(response.data.result === 'RC500'){
		toast.error(response.data.errMsg);
		setTimeout(()=>{
			localStorage.setItem('isLogin', false);
			window.location.reload();
		},3000)
	}
	return response
})

/*
get object to map
 */
export const getObject = (num) => {
	let obj = [];
	for (let i = 0; i < num; i++) {
		obj.push({})
	}
	return obj;
}


/*
check data
 */
export const checkResult = (data) => {
	return data.result === 'RC100';
}

/*
checkDate
 */
export const checkDate = (dateString) => {
	var a = /^(\d{4})-(\d{2})-(\d{2})$/;
	return a.test(dateString);
}

/*
encode
 */
export const UrlEncode = (str) => {
	let result = "";
	let special = "!\"#$%&'()*+,/:;<=>?[]^`{|}~%";
	for (let i in str) {
		if (special.indexOf(str[i]) !== -1 || (/.*[\u4e00-\u9fa5]+.*$/.test(str[i]))) {
			result += escape(str[i]).toLowerCase();
		} else {
			result += str[i];
		}
	}
	return result;
}

export const policyNoInput = (e) => {
	let policyNo = e.target.value;
	if (!/^[A-Z]/.test(policyNo)) {
		return '';
	}
	if (!/^[A-Z][0-9]*$/.test(policyNo)) {
		return policyNo[0] + policyNo.replace(/[^0-9]/ig, "");
	}
	if (!/^\S{1,10}$/.test(policyNo)) {
		return policyNo.slice(0, 10);
	}
	return policyNo;
}

/*
message text
 */
export const messageText = (string) => {
	return text[string] || string;
}
/*
toast
type : msg | loading | success | fail | offline
 */
export const toast = {
	duration: 3000,
	text(msg,duration=this.duration) {
		Toast({
			type: "msg",
			msg: msg.toString(),
			duration: duration,
		})
	},
	success(msg,duration=this.duration) {
		Toast({
			type: "success",
			msg: msg.toString(),
			duration: duration,
		})
	},
	error(msg,duration=this.duration) {
		if (msg !== undefined) {
			Toast({
				type: "fail",
				msg: msg.toString(),
				duration: duration,
			})
		}
	},
	offline(msg,duration=this.duration) {
		Toast({
			type: "offline",
			msg: msg.toString(),
			duration: duration,
		})
	},
	loading(msg,duration=this.duration) {
		Toast({
			type: "loading",
			msg: msg.toString(),
			typeStatus: 1
		})
	},
	loaded(msg,duration=this.duration) {
		Toast({
			type: "loading",
			msg: msg.toString(),
			typeStatus: 2
		})
	},

}
/*
down file
author: jichuangwei
 */
export const downFile = (data, name) => {
	var blob = new Blob([data])
	if (!!window.ActiveXObject || "ActiveXObject" in window){
		window.navigator.msSaveOrOpenBlob(blob,name+".xls")
	}else{
		 var url = window.URL.createObjectURL(blob);
		 var a = document.createElement('a');
		 a.href = url;
		 a.download = name + ".xls";
		 a.click();
	}
	
}


//时间格式转换  date,"yyyy-mm-dd HH-mm-ss"
export const format = (timestamp, type) => {
let now = new Date(timestamp);
let year = now.getFullYear();
let month = now.getMonth() + 1;
let day = now.getDate();
let hour = now.getHours();
let minute = now.getMinutes();
let second = now.getSeconds();
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
hour = hour < 10 ? "0" + hour : hour;
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;
if (type === 'y-m-d') {
return year + "-" + month + "-" + day;
}
if (type === 'y-m-d h-m') {
return year + "-" + month + "-" + day + " " + hour + ":" + minute;
}
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}

//去掉字符串前后所有空格
export const Trim = (str) => {
	return str.replace(/(^\s*)|(\s*$)/g, "");
}


//权限验证demo
export const Power=(arr1,arr2)=>{
	if(arr2===''||arr2==='0011'){
		setTimeout(()=>{
			localStorage.setItem('isLogin', false);
			window.location.reload();
		},1000)
		toast.error('该用户没有分配权限')
	}
	let list = arr2.split(',');
	for(let i in list){
		if(arr1.indexOf(list[i])!==-1){
			return true;
		}
	}
	return false;
}

//权限选择时间展示
export const datatime=(arr1)=>{
	var list1=arr1.split(',')
		if(list1.indexOf('0014')!==-1||list1.indexOf('0015')!==-1){
			return true
		}else{
			return false
		}
}


//获取结账方式年份
export const year =() => {
	let obj = []
	var dqyear =2018
	var d = new Date()
	var xyear = d.getFullYear()
	for( var i =0 ;i<10;i++){
		if(xyear>=dqyear){
          obj.push(xyear)
          xyear--;
	}
	}
	return obj
}

